//
//  KeyboardView.m
//  CAShapeLayerDemo
//
//  Created by XFB on 2017/12/1.
//  Copyright © 2017年 ZhouCheng. All rights reserved.
//

#import "KeyboardView.h"

@interface KeyboardView () <UITextViewDelegate>

@property (nonatomic, assign) NSInteger textH;  ///文字高度
@property (nonatomic, assign) NSInteger maxTextH; ///文字最大高度
@property (nonatomic, strong) UIButton *sendButton;   ///发送按钮

@end

@implementation KeyboardView

- (UITextView *)placeholderView
{
    if (_placeholderView == nil) {
        UITextView *placeholderView = [[UITextView alloc] init];
        _placeholderView.frame = CGRectMake(20, 10, self.frame.size.width-30, 30);
        _placeholderView = placeholderView;
        _placeholderView.scrollEnabled = NO;
        _placeholderView.showsHorizontalScrollIndicator = NO;
        _placeholderView.showsVerticalScrollIndicator = NO;
        _placeholderView.userInteractionEnabled = NO;
        _placeholderView.font = self.font;
        _placeholderView.textColor = [UIColor lightGrayColor];
        _placeholderView.backgroundColor = [UIColor orangeColor];
        [self addSubview:placeholderView];
    }
    return _placeholderView;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    /// 设置文本输入框 textView的属性
    self.scrollsToTop = NO;
    self.scrollEnabled = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.enablesReturnKeyAutomatically = NO; //
    self.layer.borderWidth = 1;
    self.layer.cornerRadius = 5;
    self.font = [UIFont systemFontOfSize:16];
    self.layer.borderColor = [UIColor lightGrayColor].CGColor;

    /// 输入框文本改变发送通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
}

- (void)textDidChange
{
    /// 监听文字的改变
    self.placeholderView.hidden = self.text.length > 0;
    /// 获取输入框内容的高度
    NSInteger height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, MAXFLOAT)].height);

    if (_textH != height) {
        self.scrollEnabled = height > _maxTextH && _maxTextH > 0;
        _textH = height;
    }
    
    /// block回调出去
    if (_zc_TextHeightChangeBlock && self.scrollEnabled == NO) {
        _zc_TextHeightChangeBlock(self.text, height);
        [self.superview layoutIfNeeded];
        self.placeholderView.frame = self.bounds;
    }
    
}

- (void)setMaxNumberOfRowsToShow:(int)maxNumberOfRowsToShow
{
    _maxNumberOfRowsToShow = maxNumberOfRowsToShow;
    
    /// 计算最大高度
    _maxTextH = ceil(self.font.lineHeight * maxNumberOfRowsToShow + self.textContainerInset.top + self.textContainerInset.bottom);
    
    // _maxTextH = 93;
}


- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    self.placeholderView.text = placeholder;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    self.placeholderView.textColor = placeholderColor;
}

/// 输入框的layer切割
- (void)setCornerRadius:(NSInteger)cornerRadius
{
    _cornerRadius = cornerRadius;
    self.layer.cornerRadius = cornerRadius;
}

- (void)setZc_TextHeightChangeBlock:(void (^)(NSString *, CGFloat))zc_TextHeightChangeBlock
{
    _zc_TextHeightChangeBlock = zc_TextHeightChangeBlock;
    
    [self textDidChange];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
